<?php
/*
cray 285025600@qq.com
*/

class ControllerWxpayNotify extends  Controller {

	public function index()
	{

	    $wxpayHelper = new WeixinpayHelper();
	    $notify = $wxpayHelper->verifyNotify();

	    $xml = $notify->returnXml();

	    $this->log->write($xml);

	    if($notify->checkSign() == TRUE)
	    {
	        if ($notify->data["return_code"] == "FAIL") {
	            //此处应该更新一下订单状态，商户自行增删操作
	            $this->log->write("【通信出错】:\n".$xml."\n" );
	        }
	        elseif($notify->data["result_code"] == "FAIL"){
	            //此处应该更新一下订单状态，商户自行增删操作
	            $this->log->write("【业务出错】:\n".$xml."\n" );
	        }
	        elseif($notify->data["result_code"] == "SUCCESS"){
	            //此处应该更新一下订单状态，商户自行增删操作

	            $out_trade_no = $notify->data['out_trade_no'];

	            //支付宝交易号

	            $trade_no = $notify->data['transaction_id'];


	            $this->load->model('checkout/order');
	            $order_status_id = $this->config->get('config_processing_status');
	             foreach ($order_status_id as $key=>$value)
	            {
	                $order_status_id = $value;
	            }

	            if((int)$out_trade_no > 0)
	            {

	                $this->model_checkout_order->addOrderHistory($out_trade_no, $order_status_id, '微信交易单号:'.$trade_no);
                    $order_info = $this->model_checkout_order->getOrder($out_trade_no);
	                //秒杀
	                $special =  $this->model_checkout_order->getOrderTotalByType($out_trade_no,4);

                    if($special){
                        $product = $this->model_checkout_order->getOrderProducts($out_trade_no);
                        $this->model_checkout_order->recordCustomerProductSpecial($order_info['customer_id'],$product->row['product_id'],$special['object_id']);
                    }

	                //券
	                $coupon = $this->model_checkout_order->getOrderTotalByType($out_trade_no,5);
	                if($coupon){
	                    $this->model_checkout_order->recordCustomerCoupon($order_info['customer_id'],$coupon['object_id']);
	                    $this->load->model('marketing/coupon');
	                    $this->model_marketing_coupon->addUseCouponHistory(array(
                             'coupon_id'=>$coupon['object_id'],
	                         'customer_id'=>$order_info['customer_id'],
	                         'order_id' =>$out_trade_no,
	                         'amount' =>$order_info['total']
	                    ));
	                }
	                echo 'SUCCESS';

	            }
	            else
	            {
	                $this->log->write('uncatch wxpay callback: '.(int)$out_trade_no."," .$out_trade_no .', ' .$trade_no. ', '.$notify->data["result_code"] );
	            }



	            //$this->log->write("【支付成功】:\n".$xml."\n" );
	        }
	        else
	        {
	            $this->log->write("unknown result_code:\n ".$notify->data["result_code"] ."\n".$xml."\n" );
	        }

	        //商户自行增加处理流程,
	        //例如：更新订单状态
	        //例如：数据库操作
	        //例如：推送支付完成信息
	    }

	}
}

?>